﻿using Common.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Common.Filter;

public class ValidateModelAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            var result = context.ModelState.Keys
                .SelectMany(key => context.ModelState[key].Errors.Select(x => x.ErrorMessage))
                .ToList();

            context.Result = new ObjectResult(Result.Failure(string.Join(",", result)));
        }
    }
    
}